package com.service.thirddatacenter.chiaexplorer.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.service.thirddatacenter.chiaexplorer.client.rep.BlockchainSummary;
import com.service.thirddatacenter.chiaexplorer.service.ChiaExplorerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ChiaExplorerServiceImpl implements ChiaExplorerService {

    @Value("${chia-explorer.url}")
    private String url;

    @Override
    public BlockchainSummary getBlockchainSummary() {
        HttpResponse response = HttpRequest.get(url+"/blockchainSummary").execute();
        log.info("getBlockchainSummary result:<status:{},body:{}>", response.getStatus(), response.body());
        String result = response.body();
        return StringUtils.isNotBlank(result) ? JSONObject.parseObject(result, BlockchainSummary.class) : null;
    }
}
